package com.app.songbump;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class BroadcastAudio {

	TextView connectionStatus;// = new TextView(null);
	// Video variable
	MediaRecorder recorder;
	// Networking variables
	public static String SERVERIP = "";
	public static final int SERVERPORT = 6775;
	private Handler handler = new Handler();
	private static ServerSocket serverSocket = null;
	private String path;

	Thread sendAudio;
	
	/** Called when the activity is first created. */
//http://code.google.com/p/ipcamera-for-android/source/browse/trunk/droidipcam/src/teaonly/projects/droidipcam/StreamingServer.java
	//http://developer.android.com/guide/developing/device.html

	public BroadcastAudio(String path, Context ctx, String ip) {
		connectionStatus = new TextView(ctx);

		this.path = path;
		connectionStatus = new TextView(ctx);
		this.SERVERIP = ip;
		// Run new thread to handle socket communications
		sendAudio = new Thread(new SendAudioThread());
		sendAudio.start();
		if (serverSocket == null)
			try {
				serverSocket= new ServerSocket(SERVERPORT);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public void setSong(String songPath){
		this.path = songPath;
	}
	Socket client = null;
	
	public synchronized void killSocket(){
		if (client != null){
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public class SendAudioThread implements Runnable {
		public void run() {
			// From Server.java
			try {
				if (SERVERIP != null) {
					handler.post(new Runnable() {
						@Override
						public void run() {
							connectionStatus.setText("Listening on IP: "
									+ SERVERIP);
						}
					});
					//serverSocket = new ServerSocket(SERVERPORT);
					while (true) {
						// listen for incoming clients
						try {
							client = serverSocket.accept();
						} catch (IOException e){
							return;
						}
						handler.post(new Runnable() {
							@Override
							public void run() {
								connectionStatus.setText("Connected.");
							}
						});
						try {
							// Begin audio communication
//							final ParcelFileDescriptor pfd = ParcelFileDescriptor
//									.fromSocket(client);
							OutputStream cos = client.getOutputStream();

							File song = new File(path);
							
							//String blah = path;
					    	//ByteArrayInputStream bais = new ByteArrayInputStream(song.);
					    	
							FileInputStream fis = new FileInputStream(song);
							byte[] buff = new byte[8192];
							int numChars;
							int size = 0;
							while ((numChars = fis.read(buff, 0, buff.length)) != -1) {
								cos.write(buff, 0, numChars);
								size +=numChars;
							}
							Log.v("file", path);
							Log.v("Total transferred", Integer.toString(size/1000));
							client.close();
							

						} catch (Exception e) {
							handler.post(new Runnable() {
								@Override
								public void run() {
									connectionStatus
											.setText("Oops.Connection interrupted. Please reconnect your phones.");
								}
							});
							e.printStackTrace();
						}
					}
				} else {
					handler.post(new Runnable() {

						public void run() {
							connectionStatus
									.setText("Couldn't detect internet connection.");
						}
					});
				}
			} catch (Exception e) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						connectionStatus.setText("Error");
					}
				});
				e.printStackTrace();
			}
			// End from server.java
		}
	}
}
